import modelExtend from 'dva-model-extend';
import { message } from 'antd';
import { query, create, remove, update, getDeatil } from '../services/sysUser';
import { pageModel } from './common';
import { config } from '../configs';
import { isSuccess } from '../configs/error';
import { delay } from './base';

const { prefix } = config;

export default modelExtend(pageModel, {
	namespace: 'sysUser',

	state: {
		currentItem: {},
		modalVisible: false,
		modalType: 'create',
		selectedRowKeys: [],
		isMotion: localStorage.getItem(`${prefix}userIsMotion`) === 'true',
	},

	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(location => {
				if (location.pathname === '/sysUser') {
					dispatch({
						type: 'query',
						payload: location.query,
					});
				}
			});
		},
	},

	effects: {

		*query({ payload = {} }, { call, put }) {
			if (JSON.stringify(payload) === '{}') {
				payload = { searchCount: true, openSort: true, isAsc: false, orderByField: 'utime' };
			}

			const data = yield call(query, payload);
			if (data) {
				const resultData = data.data;

				yield put({
					type: 'querySuccess',
					payload: {
						list: resultData.records,
						pagination: {
							current: Number(payload.page) || 1,
							pageSize: Number(resultData.size) || 10,
							total: resultData.total,
						},
					},
				});
			}
		},

		*'delete'({ payload }, { call, put, select }) {
			const list = yield select(state => state.sysUser.list);
			const res = yield call(remove, [payload]);
			if (isSuccess(res)) {
				message[res.data.includes('删除成功') ? 'success' : 'warn'](res.data);
				yield put({
					type: 'query',
				});
			} else {
				yield put({ type: 'list', list });
			}
		},

		*'multiDelete'({ ids }, { select, call, put }) {
			const list = yield select(state => state.sysUser.list);
			const pagination = yield select(state => state.sysUser.pagination);
			const res = yield call(remove, ids);
			if (isSuccess(res)) {
				message[res.data.includes('删除成功') ? 'success' : 'warn'](res.data);
				yield put({
					type: 'query',
				});
			} else {
				yield put({ type: 'list', list });
			}
		},

		*create({ payload }, { call, put, select, race }) {
			const creating = yield select(state => state.sysUser.creating);
			if (creating) {
				message.warning('请等待上一次创建成功');
				return;
			}
			const { data, timeout } = yield race({ // 超时控制
				data: call(create, payload),
				timeout: call(delay)
			});
			if (isSuccess(data)) {
				yield put({ type: 'hideModal' });
				yield put({ type: 'query' });
			}
		},

		*update({ payload }, { select, call, put }) {
			const id = yield select(({ sysUser }) => sysUser.currentItem.id);
			const newUser = { ...payload, id };
			const data = yield call(update, newUser);
			if (isSuccess(data)) {
				yield put({ type: 'hideModal' });
				yield put({ type: 'query' });
			}
		},

		*detail({ payload }, { call, put, select }) {
			const res = yield call(getDeatil, payload);
			const currentItem = yield select(({ sysUser }) => sysUser.currentItem);
			if (isSuccess(res)) {
				yield put({
					type: 'sysUser/showModal',  // KKK?
					payload: {
						modalType: 'update',
						currentItem: { ...currentItem, ...res.data }
					}
				});
			}
		},
	},

	reducers: {
		showModal(state, { payload }) {
			return { ...state, ...payload, modalVisible: true };
		},

		hideModal(state) {
			return { ...state, modalVisible: false };
		},

		switchIsMotion(state) {
			localStorage.setItem(`${prefix}userIsMotion`, !state.isMotion);
			return { ...state, isMotion: !state.isMotion };
		},

		remove(state, { id, ids }) {
			const list = ids
				? state.list.filter(t => !ids.includes(t.id))
				: state.list.filter(t => t.id !== id);
			return { ...state, list };
		},
	},
});
